package com.cs.study.homework._0314.list;


/*
线性表的顺序存储--->底层使用数组
 */
public class MyArrayList {
    //数组初始容量
    private static int INIT_CAPACITY = 5;
    //底层数组
    private String[] values;
    //记录容量
    private int size;

    //初始化
    public MyArrayList() {
        values = new String[INIT_CAPACITY];
    }

    //判断满
    public boolean isFull() {
        return size == INIT_CAPACITY;
    }

    //判断空
    public boolean isEmpty() {
        return size == 0;
    }


    //基于内容增
    public boolean add(String value) {
        if (isFull()) {
            grow();
        }
        values[size] = value;
        size++;
        return true;
    }

    //扩容
    public void grow() {
        String[] newStr = new String[values.length << 1];
        for (int i = 0; i < values.length; i++) {
            newStr[i] = values[i];
        }
        values = newStr;
    }

    //基于内容删
    public boolean remove(String value) {
        if (isEmpty()) {
            throw new RuntimeException("数组为空");
        }
        int index = -1;
        //找到删除元素的下标
        for (int i = 0; i < size; i++) {
            if (values[i].equals(value)) {
                index = i;
            }
        }
        if (index == -1) {
            return false;
        }

        //找到删除元素的下标后调用基于下标删除的方法
        return remove(index);
    }

    // 基于内容改
    public boolean set(String value, String newValue) {
        if (isEmpty()) {
            throw new RuntimeException("数组为空");
        }
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                values[i] = newValue;
            } else {
                throw new RuntimeException("未找到给数组元素");
            }
        }

        return true;
    }

    // 基于内容查
    public boolean contains(String value) {
        if (isEmpty()) {
            throw new RuntimeException("数组为空");
        }
        for (int i = 0; i < values.length; i++) {
            if (values[i].equals(value)) {
                return true;
            }
        }
        return false;
    }

    //基于下标增
    public boolean add(int index, String value) {
        if (isFull()) {
            grow();
        }
        if (index < 0 || index > size) {
            throw new RuntimeException("index不合法");
        }
        for (int i = size - 1; i >= index; i--) {
            values[i + 1] = values[i];
        }
        values[index] = value;
        size++;
        return true;
    }

    //基于下标删
    public boolean remove(int index) {
        if (index < 0 || index > size) {
            throw new RuntimeException("index不合法");
        }
        if (isFull()) {
            throw new RuntimeException("数组为空");
        }
        for (int i = index; i < size; i++) {
            values[i] = values[i + 1];
        }
        size--;
        return true;
    }

    //基于下标改
    public String set(int index, String value) {
        if (isFull()) {
            throw new RuntimeException("数组为空");
        }
        if (index < 0 || index > size) {
            throw new RuntimeException("index不合法");
        }
        values[index] = value;
        return value;
    }

    //基于下标查
    public String contains(int index) {
        if (isFull()) {
            throw new RuntimeException("数组为空");
        }
        return values[index];
    }
}
